package com.gdth.bank.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.utils.SharedPreferencesUtil;
import com.gdth.coder.CodeUtil;

public class TAppTokenModel {
	
	public static String currentPublicKey, currentPrivateKey;
	
	public static void newClientKey() {
		List<String> keys = CodeUtil.newPrivateKey();
		currentPrivateKey = keys.get(0);
		currentPublicKey = keys.get(1);
	}
	
	public static void savePublicKey(String publicKey, String token){
		Map<String, Object> maps = new HashMap<String, Object>();
		List<String> keys = CodeUtil.newPrivateKey();
		currentPrivateKey = keys.get(0);
		currentPublicKey = keys.get(1);
		maps.put(AppUtil.SHARE_LOGIN_ClientPublicKey, currentPublicKey);
		maps.put(AppUtil.SHARE_LOGIN_ClientPrivateKey, currentPrivateKey);
        maps.put(AppUtil.SHARE_LOGIN_ServerPublicKey, publicKey);
        maps.put(AppUtil.SHARE_DATA_TOKEN, token);
        BankApplication.mInstance.token = token;
    	
        SharedPreferencesUtil.saveSharePreferences(BankApplication.mInstance.getApplicationContext(), AppUtil.SHARE_Application, maps);
	}
	

	public static String getPublicKey(){
		return SharedPreferencesUtil.getSharePreferencesByKey(BankApplication.mInstance.getApplicationContext(), AppUtil.SHARE_Application, AppUtil.SHARE_LOGIN_ClientPublicKey);
	}
	
	public static String getPrivateKey(){
		return SharedPreferencesUtil.getSharePreferencesByKey(BankApplication.mInstance.getApplicationContext(), AppUtil.SHARE_Application, AppUtil.SHARE_LOGIN_ClientPrivateKey);
	}
	
	public static String getPublicKeyForServer(){
		return SharedPreferencesUtil.getSharePreferencesByKey(BankApplication.mInstance.getApplicationContext(), AppUtil.SHARE_Application, AppUtil.SHARE_LOGIN_ServerPublicKey);
	}
	
	public static String getCurrentToken(){
		return SharedPreferencesUtil.getSharePreferencesByKey(BankApplication.mInstance.getApplicationContext(), AppUtil.SHARE_Application, AppUtil.SHARE_DATA_TOKEN);
	}
	
	public static void resetToken(){
		SharedPreferencesUtil.saveSharePreference(BankApplication.mInstance.getApplicationContext(), AppUtil.SHARE_Application, AppUtil.SHARE_DATA_TOKEN, "");
	}
}
